//package com.ruoyi.web.task;
//
//import com.mongodb.client.result.DeleteResult;
//import com.ruoyi.system.domain.MongoArchive;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.data.mongodb.core.query.Query;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.annotation.Resource;
//import java.text.SimpleDateFormat;
//import java.time.Instant;
//import java.time.LocalDate;
//import java.time.format.DateTimeFormatter;
//import java.time.temporal.ChronoUnit;
//import java.util.Date;
//
//@EnableScheduling
//@Configuration
//@Slf4j
//public class MongoArchiveHistoryTask {
//
//    @Resource
//    private MongoTemplate mongoTemplate;
//
//    @Scheduled(cron = "0 23 12 * * ?")
//    public void deleteCategoryHistory(){
//        SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Instant threeDaysAgoInstant = Instant.now().minus(3, ChronoUnit.DAYS);
//        Date date = Date.from(threeDaysAgoInstant);
//        log.info("删除mongodb分类存档历史开始，将删除" + dtf.format(date) + "以前的数据");
//        Integer i = 0;
//        while (true) {
//            // 每次查询10000条
//            Query query = new Query(Criteria.where("createdTime").lt(date)).limit(10000);
//            DeleteResult deleteResult = mongoTemplate.remove(query, MongoArchive.class);
//            long deletedCount = deleteResult.getDeletedCount();
//            // 如果本次删除少于10000条，说明已删除完毕
//            if (deletedCount < 10) {
//                break;
//            }
//            try {
//                Thread.sleep(50);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            log.info("数据累计删除" + ((++i) * 10000));
//        }
//        log.info("删除mongodb分类存档历史结束");
//    }
//}
